package com.android.fastride.present;

import android.os.Bundle;
import android.view.View;

import com.android.fastride.activity.ProxyFragmentActivity;
import com.android.fastride.contract.MessageListContract;
import com.android.fastride.model.db.entity.FastRideMessage;
import com.android.fastride.model.repository.ControlSystemRepository;
import com.android.fastride.view.fragment.MessageDetailFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.peng.android.coremodule.utils.RxSchedulerUtils;
import com.peng.library.mantis.expand.list.MantisListPresent;
import com.peng.library.mantis.mvp.PresentEvent;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;

public class MessageListPresent extends MantisListPresent<MessageListContract.VIEW,
        FastRideMessage.NoticeBean>
        implements MessageListContract.PRESENT {
    private static final String TAG = "MessageListPresent";
    //    private FastRideMessage mFastRideMessage;
    private int mRow = 10;
    private int mPage = 1;
    private boolean isRefreshCall = false;
    private boolean isHasNextPage = false;
    private List<FastRideMessage.NoticeBean> testDataList = new ArrayList<>(15);

    @Override
    public void onCreate(MessageListContract.VIEW view, Bundle data) {
        super.onCreate(view, data);
    }

    @Override
    public void onCreateView(MessageListContract.VIEW view) {
        super.onCreateView(view);
    }

    @Override
    public void onRefreshCall() {
        restLoadMore();
        mPage = 1;
        isRefreshCall = true;
        loadData();
    }

    @Override
    public void onLoadMoreRequested() {

        if(!isHasNextPage) {
            getAdapter().loadMoreEnd();
            return;
        }
        loadData();
    }

    private void updateFastRideMessage(FastRideMessage fastRideMessage) {
        isHasNextPage = fastRideMessage.isHasNextPage();
        if (isHasNextPage) {
            mPage = fastRideMessage.getNextPage();
            getAdapter().loadMoreComplete();
        } else {
            getAdapter().loadMoreEnd();
        }
    }

    private void loadData() {
        ControlSystemRepository.getNotice(mRow, mPage)
                .compose(RxSchedulerUtils.<FastRideMessage>normalSchedulersTransformer())
                .compose(this.bindUntilEvent(PresentEvent.DESTROY))
                .subscribe(new Subscriber<FastRideMessage>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        getViewDelegate().showErrorView("请求出错了！", e.getMessage());
                    }

                    @Override
                    public void onNext(FastRideMessage fastRideMessage) {
                        // 刷新时，列表数据为空时显示emptyview
                        if(isRefreshCall && fastRideMessage.getList().size() == 0) {
                            getViewDelegate().showEmptyView();
                            return;
                        }

                        updateFastRideMessage(fastRideMessage);
                        if (isRefreshCall) {
                            // 刷新时的请求
                            testDataList.clear();
                            testDataList.addAll(fastRideMessage.getList());
                            publishListData(testDataList);
                            isRefreshCall = false;
                        } else {
                            getAdapter().addData(fastRideMessage.getList());
                            getAdapter().notifyDataSetChanged();
                        }
                    }
                });
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(MessageDetailFragment.PARA, testDataList.get(position));
        ProxyFragmentActivity.start(getActivity(), MessageDetailFragment.class, bundle);
    }

    @Override
    public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
        return false;
    }



}
